package com.ghck.server.dao.provider;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.ghck.server.model.TestCategoryDict;
import com.ghck.server.model.query.TestCategoryDictQuery;
import com.ghck.server.utils.IntegerUtil;
/**
 * 健康自测类型-支持管理平台配置  <br>
 * dict_test_category <br>
 * v1.0 2016-05-01
 * @see com.ajk.server.dao.TestCategoryDictMapper
 */
 public class TestCategoryDictProvider {
 
 	/**
 	* 新增 健康自测类型-支持管理平台配置
 	*/
 	public String insert(TestCategoryDict domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("insert into dict_test_category(create_time,last_modify_time");
	 	if(domain.getName()!=null){
			sb.append(",name");
		}
	 	if(domain.getParentId()!=null){
			sb.append(",parent_id");
		}
	 	if(domain.getTestAmount()!=null){
			sb.append(",test_amount");
		}
	 	if(domain.getShareAmount()!=null){
			sb.append(",share_amount");
		}
	 	if(domain.getTimeLimit()!=null){
			sb.append(",time_limit");
		}
	 	if(domain.getInUsed()!=null){
			sb.append(",in_used");
		}
	 	if(domain.getScore()!=null){
			sb.append(",score");
		}
	 	if(domain.getSeq()!=null){
	 		sb.append(",seq");
	 	}
		
		sb.append(") values ( now(),now() ");
		
	 	if(domain.getName()!=null){
			sb.append(",#{name}");
		}
	 	if(domain.getParentId()!=null){
			sb.append(",#{parentId}");
		}
	 	if(domain.getTestAmount()!=null){
			sb.append(",#{testAmount}");
		}
	 	if(domain.getShareAmount()!=null){
			sb.append(",#{shareAmount}");
		}
	 	if(domain.getTimeLimit()!=null){
			sb.append(",#{timeLimit}");
		}
	 	if(domain.getInUsed()!=null){
			sb.append(",#{inUsed}");
		}
	 	if(domain.getScore()!=null){
			sb.append(",#{score}");
		}
	 	if(domain.getSeq()!=null){
	 		sb.append(",#{seq}");
	 	}
		sb.append(")");
		
		return sb.toString();
	}

	/**
 	* 更新 健康自测类型-支持管理平台配置
 	*/
	public String update(TestCategoryDict domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("update dict_test_category set last_modify_time = now() ");
		
		if(domain.getName()!=null){
			sb.append(",name=#{name}");
		}
		if(domain.getParentId()!=null){
			sb.append(",parent_id=#{parentId}");
		}
		if(domain.getTestAmount()!=null){
			sb.append(",test_amount=#{testAmount}");
		}
		if(domain.getShareAmount()!=null){
			sb.append(",share_amount=#{shareAmount}");
		}
		if(domain.getTimeLimit()!=null){
			sb.append(",time_limit=#{timeLimit}");
		}
		if(domain.getInUsed()!=null){
			sb.append(",in_used=#{inUsed}");
		}
	 	if(domain.getScore()!=null){
			sb.append(",score=#{score}");
		}
		if (domain.getSeq()!=null) {
			sb.append(",seq=#{seq}");
		}
		sb.append(" where id=#{id}");
		
		return sb.toString();
	}
	
	public String batchInsert(Map<String, List<TestCategoryDict>> map){
		StringBuffer sb = new StringBuffer();
		List<TestCategoryDict> testCategoryDicts = map.get("list");
		sb.append("insert into dict_test_category(create_time,last_modify_time,name,parent_id,test_amount,share_amount,time_limit,in_used,score,seq) values");
	    MessageFormat messageFormat = new MessageFormat("("
	    		+ "now(),now()"
	    		+ ",#'{'list[{0}].name}"
	    		+ ",#'{'list[{0}].parentId}"
	    		+ ",#'{'list[{0}].testAmount}"
	    		+ ",#'{'list[{0}].shareAmount}"
	    		+ ",#'{'list[{0}].timeLimit}"
	    		+ ",#'{'list[{0}].inUsed}"
	    		+ ",#'{'list[{0}].score}"
	    		+ ",#'{'list[{0}].seq}"
	    		+")");
	    		
	    if(CollectionUtils.isNotEmpty(testCategoryDicts)) {
			sb.append(messageFormat.format(new Integer[]{0}));
			for (int i = 1; i <  testCategoryDicts.size(); i++) {
				sb.append(",").append(messageFormat.format(new Integer[]{i}));
			}
		}
		return sb.toString();
	}
	
	/**
 	*通过query 查询 健康自测类型-支持管理平台配置
 	*/
	public String findByQuery(TestCategoryDictQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select * from dict_test_category a where 1 = 1 ");
		if(query.getParentId() != null){
			sb.append(" and parent_id = #{parentId} ");
		}
		if(query.getCategoryId() != null && query.getCategoryId() != 0){
			sb.append(" and category_id = #{categoryId} ");
		}
		if(StringUtils.isEmpty(query.getSortStatment())){
			sb.append(" order by seq desc ");
		}else{
			sb.append(" ").append(query.getSortStatment());
		}
		if(query.getStart()!=null && query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" limit #{start},#{limit}");
		}else if(query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" limit #{limit}");
		}
		return sb.toString();
	}
	
	public String countByQuery(TestCategoryDictQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select count(1) from dict_test_category a ");
		return sb.toString();
	}
	
	/**
 	*通过ids 查询 健康自测类型-支持管理平台配置
 	*/
 	public String findByIds(Map<String, List<Integer>> map){
		StringBuffer sb = new StringBuffer();
		List<Integer> ids = map.get("list");
		sb.append("select * from dict_test_category where ");
		if(ids==null || ids.isEmpty()){
			sb.append(" 1<2");
		}else{
			if(ids.size()==1){
				sb.append(" id=").append("#{list[0]}");
			}else{
				sb.append(" id in (").append("#{list[0]}");
				for(int i=1; i<ids.size() ; i++){
					sb.append(",").append("#{list[").append(i).append("]}");
				}
				sb.append(")");
			}
		}
		return sb.toString();
	}
	
 	public String findByParentId(Map<String, Integer> map){
 		StringBuilder sb = new StringBuilder();
 		Integer start = map.get("start");
 		Integer limit = map.get("limit");
 		sb.append("select * from dict_test_category where in_used=1 and parent_id=#{parentId}");
 		sb.append(" order by seq desc");
 		if(start!=null && IntegerUtil.isPositiveInteger(limit)){
			sb.append(" limit #{start},#{limit}");
		}else if(IntegerUtil.isPositiveInteger(limit)){
			sb.append(" limit #{limit}");
		}
		return sb.toString();
 	}
 }	